//
//  ShowRoomViewController.m
//  XiRan-iOS
//
//  Created by 韩强 on 12/31/19.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "ShowRoomViewController.h"
#import "ShowRoomListCollectionViewCell.h"
#import "MWShowroomController.h"

@interface ShowRoomViewController () <UICollectionViewDataSource, UICollectionViewDelegate, ShowRoomListCollectionViewCellDelegate>

/// 列表展示
@property(strong, nonatomic) UICollectionView *collectionView;

/// 数据源
@property(nonatomic, strong) NSMutableArray *showroomArray;

@property(nonatomic, assign) NSInteger page;
@property(nonatomic, assign) NSInteger size;
@property(nonatomic, assign) MWRequestMode requestMode;
@end

@implementation ShowRoomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.collectionView.mj_header beginRefreshing];
}

- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [self.view addSubview:_collectionView];
        
        [_collectionView registerClass:[ShowRoomListCollectionViewCell class] forCellWithReuseIdentifier:@"ShowRoomListCollectionViewCell"];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(self.view);
        }];
        
        MJRefreshNormalHeader *refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self refreshApplication];
        }];
        refreshHeader.stateLabel.hidden = YES;
        _collectionView.mj_header = refreshHeader;
        
        MJRefreshBackNormalFooter *refreshFooter = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self loadMoreApplication];
        }];
        refreshFooter.stateLabel.hidden = YES;
        _collectionView.mj_footer.hidden = YES;
        _collectionView.mj_footer = refreshFooter;
        
    }
    return _collectionView;
}


- (void)refreshApplication {
    _page = 1;
    _size = 20;
    _requestMode = MWRequestModeRefresh;
    [self upData];
}

- (void)loadMoreApplication {
    _page++;
    _size = 20;
    _requestMode = MWRequestModeLoadMore;
    [self upData];
}

- (NSMutableArray *)showroomArray {
    if (_showroomArray == nil) {
        _showroomArray = [NSMutableArray array];
    }
    return _showroomArray;
}


#pragma mark - 请求当前页面的数据
- (void)upData {
    NSString *token = [MWUserManager manager].user.token;
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:token forKey:@"token"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    [paraDic setObject:[NSString stringWithFormat:@"%ld", (long)self.page] forKey:@"page"];
    [paraDic setObject:[NSString stringWithFormat:@"%ld", (long)self.size] forKey:@"size"];
    [[MWNetworkManager manager] POST:kShowroomListUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        if (code == 1) {
            NSArray *tempArray = [MWShowroomModel mj_objectArrayWithKeyValuesArray:[dic objectForKey:@"data"]];
    
            if (self.requestMode == MWRequestModeRefresh) {
                [self.collectionView.mj_header endRefreshing];
                [self.showroomArray removeAllObjects];
                if (tempArray.count == self->_size) {
                    self.collectionView.mj_footer.hidden = NO;
                }
                
            } else {
                if (tempArray.count < self->_size) {
                    [self.collectionView.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [self.collectionView.mj_footer endRefreshing];
                }
            }
            
            [self.showroomArray addObjectsFromArray:tempArray];
            [self.collectionView reloadData];
            
        } else {
            [self.collectionView.mj_header endRefreshing];
            [self.collectionView.mj_footer endRefreshing];
        }
    } failure:^(NSError *error) {
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        [self.collectionView reloadData];
    }];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"ShowRoomListCollectionViewCell";
    ShowRoomListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.delegate = self;
    cell.showroomModel = self.showroomArray[indexPath.row];
    return cell;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if ([MWConfigManager manager].isNetError) {
        self.errorTitleLabel.text = [[MWConfigManager manager] objectForKey:@"click_retry" defaultValue:@"点击重试"];
        self.errorTitleLabel.hidden = self.errorImageView.hidden = NO;
        collectionView.hidden = YES;
    } else {
        self.errorTitleLabel.hidden = self.errorImageView.hidden = YES;
        collectionView.hidden = NO;
    }
    return self.showroomArray.count;
}


- (void)refresh {
    [super refresh];
    [self upData];
}

#pragma mark - 查看更多的监听事件
- (void)ShowRoomListCollectionViewCellLookMoreButtonClickWith:(MWShowroomModel *)showroomModel {
    MWShowroomController *sc = [[MWShowroomController alloc] init];
    sc.showroom = showroomModel;
    [self.navigationController pushViewController:sc animated:YES];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}



#pragma mark - UICollectionViewDelegateFlowLayout
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 11;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 16, 0, 16);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(kScreenWidth, 15);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((kScreenWidth - 47) / 2, 185);
}



@end
